package archimede;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Properties;

import javax.imageio.ImageIO;
import javax.swing.JLabel;

public class Salva {
	private Pannello pan;
	private JLabel info;
	private int count;

	public Salva(Pannello p, int count) {
		this.pan = p;
		this.info = this.pan.getInfo();
		this.count = count;
	}

	public void setInfo(JLabel inf) {
		this.info = inf;
	}

	public void salvaImmaginePNG(String name) {
		Properties pc = System.getProperties();
		String homePath = pc.getProperty("user.home");
		String directoryProject = homePath + pc.getProperty("file.separator")
				+ "ArchimedeSuper" + pc.getProperty("file.separator") + count
				+ pc.getProperty("file.separator");
		File file = new File(directoryProject + name);
		String nome = file.getPath();
//		System.out.println(nome);
		if (!nome.endsWith(".png")) {
			nome = nome + ".png";
			file = new File(nome);
		}

		try {
			int width = this.pan.XSfondo;
			int height = this.pan.YSfondo;
//			System.out.println("x "+width+" y"+height);
			BufferedImage startImg = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			Graphics2D g = (Graphics2D) startImg.getGraphics();
			this.pan.paint(g);
			g.dispose();

			BufferedImage myImg = startImg.getSubimage(0, 0, width, height);
			
			ImageIO.write(myImg, "png", file);
//			System.out.println("write");
//			this.info.setText("Ho scritto l'immagine " + file.getName());

		} catch (Exception ecc) {
//			this.info.setText("Errore nella scrittura dell'immagine");
			ecc.printStackTrace();
		}

	}
}